import Vue from 'vue'
import VueRouter from 'vue-router'
import User from '../components/User'
Vue.use(VueRouter)

const routes = [
  {
    path:'',
    redirect:'/home',
    meta:{
      title:'首页'
    }
  },
  {
    path:'/user/:userid',
    component:User,
    meta:{
      title:'用户'
    }
  },
  {
    path: '/home',
    name: 'home',
    component: ()=>import('../views/Home'),
    meta:{
      title:'首页'
    },
    children:[
      {
        path:'child',
        component:()=>import('../components/Child'),
        meta:{
          title:'子组件'
        }
      }
    ]
  },
  // 路由懒加载的方式
  {
    path: '/about',
    name: 'about',
    meta:{
      title:'关于'
    },
    component: () => import('../views/About.vue')
  },
  {
    path: '/profile',
    name: 'profile',
    meta:{
      title:'档案'
    },
    component: () => import('../components/Profile.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局导航守卫改变title
router.beforeEach((to,from,next) =>{
  console.log(window.localStorage.getItem('title'))
  document.title = to.meta.title
  next()
})
export default router
